package com.guoguo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.guoguo.base.PageBean;
import com.guoguo.entity.*;
import com.guoguo.entity.vo.*;
import com.guoguo.mapper.*;
import com.guoguo.service.AlbumService;
import com.guoguo.utils.AliOssUtils;
import com.guoguo.utils.aliOss.AliOssImageConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by Administrator on 2017/8/28.
 */
@Service
public class AlbumServiceImpl implements AlbumService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AlbumMapper albumMapper;
    @Autowired
    private ImagesMapper imagesMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    private AcCategoryAlbumMapper acCategoryAlbumMapper;

    @Autowired
    private AcAlbumImageMapper acAlbumImageMapper;


    @Autowired
    private AcModelAlbumMapper acModelalbumMapper;

    @Override
    public PageBean<AlbumsVO> selectAlbumList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageBean<AlbumsVO>(albumMapper.selectAlbumList());
    }

    /**
     * 查看专辑收藏列表
     *
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<FavoriteVO> getAlbumList(String userId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<FavoriteVO> favoritesVOs = favoriteMapper.getAlbumList(userId, pageNum, pageSize);
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        for (FavoriteVO favoritesVO : favoritesVOs) {
            if (favoritesVO.getModelIcon() != null || !"".equals(favoritesVO.getModelIcon())) {
                favoritesVO.setModelIcon(AliOssUtils.getImageUrl(favoritesVO.getModelIcon()));//, aliOssImageConfig));
            }
            if (favoritesVO.getCoverUrl() != null || !"".equals(favoritesVO.getCoverUrl())) {
                favoritesVO.setCoverUrl(AliOssUtils.getImageUrl(favoritesVO.getCoverUrl()));//, aliOssImageConfig));
            }

        }
        return new PageBean<FavoriteVO>(favoritesVOs);
    }


    @Override
    public List<Album> selectBy(Album albums) {
        return albumMapper.selectBy(albums);
    }

    @Override
    public List<Album> getAlbum() {
        return albumMapper.getAlbum();
    }

    /**
     * 专辑删除
     *
     * @param albId
     * @return
     */
    public int deleteAlbumInfo(String albId) {
        int countNum = 0;
        AlbumsVO album = albumMapper.selectByPrimaryKey(albId);
        if (album != null) {
            //图片关联删除
            List<AcAlbumImage> acAlbumImages = acAlbumImageMapper.getAcAlbumImage(albId);

            if (acAlbumImages.size() > 0) {
                List<String> imageIdList = new ArrayList<>();
                for (AcAlbumImage acAlbumImage : acAlbumImages) {
                    imageIdList.add(acAlbumImage.getImgId());
                }
                int imgNum = imagesMapper.deleteImages(imageIdList);
                int acAlbumImageNum = acAlbumImageMapper.deleteByAblId(albId);
                countNum += imgNum;
                countNum += acAlbumImageNum;
                logger.info("================>>>>>   删除图片数量：" + imgNum + "。删除图片专辑关联数量：" + acAlbumImageNum);
            }
            //删除云图片


            //模特关联删除
            AcModelAlbums acModelAlbum = new AcModelAlbums();
            acModelAlbum.setModelId(album.getModelId());
            acModelAlbum.setAlbId(albId);
            List<AcModelAlbums> modelAlbums = acModelalbumMapper.findAcModelAlbum(acModelAlbum);
            if (modelAlbums.size() > 0) {
                int acModelAlbumNum = acModelalbumMapper.deleteAcModelAlbumById(albId);
                logger.info("================>>>>>   删除关联模特数量：" + acModelAlbumNum);
                countNum += acModelAlbumNum;
            }
            //专辑关联删除
            List<AcCategoryAlbum> acCategoryAlbumList = acCategoryAlbumMapper.findCategoryAlbumByAlbId(albId);
            if (acCategoryAlbumList.size() > 0) {
                int acCategoryNum = acCategoryAlbumMapper.deleteAcCategoryAlbums(albId);
                logger.info("================>>>>>   删除关联分类数量：" + acCategoryNum);
                countNum += acCategoryNum;
            }

            int albumNum = albumMapper.deleteByPrimaryKey(albId);
            logger.info("================>>>>>   删除专辑数量：" + albumNum);
            countNum += albumNum;
            logger.info("================>>>>>   删除数据总数量：" + countNum);
        }
        return countNum;
    }

    /**
     * 添加/修改专辑
     *
     * @param albId
     * @param albName
     * @param albDescription
     * @param authorId
     * @param modelId
     * @param cateIds
     * @param imgIds
     * @param converIds
     * @return
     */
    @Override
    public boolean addAlbum(String albId, String albName, String albDescription,String originalPrice,String presentPrice, String authorId, String modelId, String cateIds, String imgIds, String converIds) {
        //添加/修改专辑
        AlbumsVO album = albumMapper.selectByPrimaryKey(albId);
        if (album == null) {
            album = new AlbumsVO(albId, albName, true, albDescription,originalPrice,presentPrice, authorId, modelId, new Date(), new Date(), "1");
            album.setCommentNum(Long.valueOf(0));
            album.setFavoriteNum(Long.valueOf(0));
            album.setShareNum(Long.valueOf(0));
            albumMapper.insert(album);
        } else {
            album.setAlbName(albName);
            album.setAlbDescription(albDescription);
            album.setModelId(modelId);
            album.setUpdateTime(new Date());
            album.setOriginalPrice(originalPrice);
            album.setPresentPrice(presentPrice);
            albumMapper.updateByPrimaryKeySelective(album);
        }


        String[] cateIdArr = cateIds.split(",");
        //添加/修改分类和专辑关联
        JSONObject conver = JSON.parseObject(converIds);
        int acca = 0;
        for (int i = 0; i < cateIdArr.length; i++) {
            AcCategoryAlbum acCategoryAlbum = new AcCategoryAlbum();
            acCategoryAlbum.setCateId(cateIdArr[i]);
            acCategoryAlbum.setAlbId(albId);

            String converId = conver.getString(cateIdArr[i]);
            Images images = imagesMapper.selectByPrimaryKey(converId);
            acCategoryAlbum.setCoverUrl(images.getOriginalUrl());

            AcCategoryAlbum ac = acCategoryAlbumMapper.findCategoryAlbum(acCategoryAlbum);
            if (ac == null) {
                acca = acCategoryAlbumMapper.addAcCategoryAlbum(acCategoryAlbum);
            } else {
                acca = acCategoryAlbumMapper.updateActegoryAlbum(acCategoryAlbum);
            }
        }
        //增加或者修改模特和专辑关联
        AcModelAlbums acModelAlbum = new AcModelAlbums();
        acModelAlbum.setModelId(modelId);
        acModelAlbum.setAlbId(albId);
        List<AcModelAlbums> modelAlbums = acModelalbumMapper.findAcModelAlbum(acModelAlbum);
        if (modelAlbums.size() > 0) {
            acModelalbumMapper.updateAcModelAlbum(acModelAlbum);
        } else {
            acModelalbumMapper.saveAcModelAlbum(acModelAlbum);
        }

        int img = 0;
        String[] imgIdArr = imgIds.split(",");
        for (int i = 0; i < imgIdArr.length; i++) {
            Images images = imagesMapper.selectByPrimaryKey(imgIdArr[i]);
            images.setIsFree(0);
            acca = imagesMapper.updateByPrimaryKeySelective(images);
        }
        boolean flag = false;
        if (img > 0 || acca > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public PageBean<CommentVO> getAlbumComment(String albId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        List<CommentVO> list = commentMapper.getAlbumComment(albId);
        for (CommentVO commentVO : list) {
            commentVO.setIcon(AliOssUtils.getImageUrl(commentVO.getIcon()));//, aliOssImageConfig));
        }
        return new PageBean<CommentVO>(list);

    }

    @Override
    public PageBean<CommentVO> getHotAlbumComment(String albId, int pageNum, int pageSize) {
        PageHelper.startPage(1, 3);
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        List<CommentVO> list = commentMapper.getHotAlbumComment(albId);
        for (CommentVO commentVO : list) {
            commentVO.setIcon(AliOssUtils.getImageUrl(commentVO.getIcon()));//, aliOssImageConfig));
        }
        return new PageBean<CommentVO>(list);
    }

    @Override
    public int insert(Album albums) {
        return albumMapper.insert(albums);
    }

    @Override
    public int insertSelective(Album albums) {
        return albumMapper.insertSelective(albums);
    }

    @Override
    public int countBy(Album albums) {
        return albumMapper.countBy(albums);
    }

    @Override
    public AlbumsVO selectByPrimaryKey(String albId) {
        return albumMapper.selectByPrimaryKey(albId);
    }

    @Override
    public int deleteByPrimaryKey(String albId) {
        return albumMapper.deleteByPrimaryKey(albId);
    }

    @Override
    public int deleteBy(Album albums) {
        return albumMapper.deleteBy(albums);
    }

    @Override
    public int updateBySelective(Album albums) {
        return albumMapper.updateBySelective(albums);
    }

    @Override
    public int updateBy(Album albums) {
        return albumMapper.updateBy(albums);
    }

    @Override
    public int updateByPrimaryKeySelective(Album albums) {
        return albumMapper.updateByPrimaryKeySelective(albums);
    }

    @Override
    public int updateByPrimaryKey(String albId) {
        return albumMapper.updateByPrimaryKey(albId);
    }

    @Override
    public int UDByPrimaryKey(String albId) {
        return albumMapper.UDByPrimaryKey(albId);
    }
}
